package com.tojabo.sound.ogg;

import java.util.List;

import com.tojabo.entity.DoReMiFa;
import com.tojabo.util.FileUtil;
import com.tojabo.util.SoundUtil;

public class MainTest {
	private static SinglePlayerManager manager;

	public static void main(String[] args) {
		manager = SinglePlayerManager.getIntance();

		String filename = "E:/sky-city.txt";//E:/hidden_youself.txt  the_dawn.txt sky-city.txt
		List<String> list = FileUtil.readLines(filename);

		for (String pitch : list) {
			DoReMiFa doremifa = SoundUtil.parserMusicBook(pitch);
			manager.pushSound(doremifa);
		}

		
		DoReMiFa nowDoRe =manager.pollSound();
		startPlay(nowDoRe);
	}

	public static void startPlay(DoReMiFa nowDoRe) {
		// 2 获取下个一播放的音
		DoReMiFa nextDoRe = manager.pollSound();
		if (nextDoRe != null) {
			PlayThread nextThread = new PlayThread(nextDoRe,
					nowDoRe.getDurationTime());
			nextThread.start();
		}

		// 3 播放
		NewOggPlayer player = manager.pollOggPlayer();
		System.out.println(manager.addSoundIndex()+":"+nowDoRe.getPitchName());
		player.play(nowDoRe.getPitchFile());

		// 4 用完了 还回去
		manager.rebackOggPlayer(player);
	}

	public void playAllSound() {
		String pitchfile = "E:/pitchName.txt";
		String content = FileUtil.fileReader(pitchfile);

		String[] soundFileArray = content.split("\r\n");

		NewOggPlayer oggPlayer = new NewOggPlayer();

		for (String sound : soundFileArray) {
			System.out.println(sound);
			oggPlayer.play(sound);
		}
		oggPlayer.close();
	}

	public void writeToFile() {
		String pitchNames = "a0,a0m,b0,"
				+ "c1,c1m,d1,d1m,e1,f1,f1m,g1,g1m,a1,a1m,b1,"
				+ "c2,c2m,d2,d2m,e2,f2,f2m,g2,g2m,a2,a2m,b2,"
				+ "c3,c3m,d3,d3m,e3,f3,f3m,g3,g3m,a3,a3m,b3,"
				+ "c4,c4m,d4,d4m,e4,f4,f4m,g4,g4m,a4,a4m,b4,"
				+ "c5,c5m,d5,d5m,e5,f5,f5m,g5,g5m,a5,a5m,b5,"
				+ "c6,c6m,d6,d6m,e6,f6,f6m,g6,g6m,a6,a6m,b6,"
				+ "c7,c7m,d7,d7m,e7,f7,f7m,g7,g7m,a7,a7m,b7," + "c8";

		String filepath = "E:/tutorial/java_sound/ogg_data";

		String[] nameArrays = pitchNames.split(",");

		StringBuffer sbf = new StringBuffer();

		for (String pitch : nameArrays) {
			sbf.append(filepath + "/" + pitch + ".ogg" + "\r\n");
		}

		System.out.println(sbf.toString());
		String pitchfile = "E:/pitchName.txt";
		FileUtil.fileWriter(pitchfile, sbf.toString());
	}
}
